package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacherfront")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;

    //3.分页查询讲师列表
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("getTeacherFrontList/{current}/{limit}")
    public R getTeacherFrontList(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name="limit",value = "页面数大小",required = true)
            @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        //查询分页对象
        Map<String,Object> map =teacherService.getTeacherFrontList(teacherPage);
        return R.ok().data(map);  //链式编程也可以
    }

    //6.根据讲师id进行查询
    @ApiOperation(value = "讲师id进行查询")
    @GetMapping("getFrontTeacherInfo/{id}")
    public R getTeacher( @ApiParam(name="id",value = "讲师ID",required = true)
                         @PathVariable String id) {
        //根据id查出讲师个人信息
        EduTeacher eduTeacher = teacherService.getById(id);

        //根据讲师id查询课程信息
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.eq("teacher_id",id);
        List<EduCourse> eduList = courseService.list(wrapper);

        return R.ok().data("teacher",eduTeacher).data("courseList",eduList);
    }
}
